package com.globefrog.framework.log.util;

/**
 * 只读对象
 *
 * @author : caijinbang
 * @date : Create in 2021-01-16
 */
public class ReadOnly<T> {

  /**
   * 对象.
   */
  private T value;
  /**
   * 是否分配.
   */
  private boolean assigned;

  public ReadOnly() {
  }

  public boolean assigned() {
    return this.assigned;
  }

  public T value() {
    return this.value;
  }

  public void set(T value) {
    if (this.assigned) {
      throw new IllegalStateException(
          "value has been assigned, oldValue=" + this.value + ", newValue=" + value);
    } else {
      this.assigned = true;
      this.value = value;
    }
  }

  public boolean valueEquals(T otherValue) {
    return this.value == null && otherValue == null || otherValue != null && otherValue
        .equals(this.value);
  }

  public String toString() {
    return String.valueOf(this.value);
  }

}
